layui.define(['tableExt', 'xmSelect',"laydate"], function (exports) {
    laydate = layui.laydate;
    table = layui.table;
    form = layui.form;
    // 日期时间范围
    laydate.render({
        elem: '#orderDateRangeDatetime',
        type: 'datetime',
        range: ['#orderDateFrom', '#orderDateTo']
    });
    // 渲染
    table.render({
        elem: '#mallOrderTable',
        url: ctx + '/ajaxProxy/sales.oms.detail.ui/mallOrderList/getMallOrderList',
        cols: [function () {
            var arr = [
                {checkbox: true, fixed: true},
                {field: 'b2SupplementalStatusStr', title: 'B2结算状态', width: 140},
                {field: 'salesOrderStatusStr', title: '状态', width: 140},
                {field: 'orderTypeStr', title: '类型', width: 140},
                {field: 'orderSourceStr', title: '来源', width: 140},
                {
                    field: 'orderCode', title: '单号', width: 180, templet: function (d) {
                        return `<div></div><a href="${d.newOrderDetailUrl}" target="_blank" style="text-decoration: underline;">${d.orderCode}</a></div>`;
                    }
                },
                {field: 'buyerOrderCode', title: '报价单号', width: 180},
                {field: 'traderName', title: '交易员', width: 140},
                {field: 'customerName', title: '采购方', width: 180},
                {field: 'customerDepartment', title: '客户部门', width: 140},
                {field: 'salesTotalPieces', title: '数量', width: 140},
                {field: 'salesTotalWeight', title: '重量', width: 140},
                {field: 'b2TotalAmount', title: '预收金额', width: 140},
                {field: 'isUseIouStr', title: '是否使用白条', width: 140},
                {field: 'isUseTpCreditStr', title: '是否使用企信', width: 140},
                {field: 'actualB2TotalAmount', title: '实提应收', width: 140},
                {field: 'amountAndDeposit', title: '凭证使用金额', width: 140},
                {field: 'actualTotalPiece', title: '实发数量', width: 140},
                {field: 'actualTotalWeight', title: '实发重量', width: 140},
                {field: 'stockOwnerName', title: '货主', width: 140},
                {field: 'b2InvoiceStatus', title: '发票', width: 140},
                {field: 'orderDate', title: '下单时间', width: 180},
                {field: 'contactName', title: '联系人', width: 140},
                {field: 'contactPhone', title: '联系人电话', width: 140},
                {field: 'lastModifiedTime', title: '最后更新', width: 180},
                {field: 'remark', title: '备注', width: 140},
                {field: 'departmentName', title: '部门', width: 140},
                {field: 'branchCompanyName', title: '分公司', width: 140},
                {field: 'b2SupplementalDate', title: '补退时间', width: 140},
                {field: 'addedIp', title: '下单IP', width: 140},
                {field: 'contractOutCode', title: '外部合同号', width: 140},
                {field: 'b1SupplementalStatusStr', title: 'B1结算状态', width: 140},
                {field: 'amountPaid', title: '供应支款', width: 140},
                {field: 'actualB1TotalAmount', title: '实提应付', width: 140},
                {field: 'orgTitleName', title: '本方公司', width: 240},
                {field: 'investorName', title: '供应商', width: 240},
                {field: 'supplierName', title: '货源', width: 240},
                {field: 'bizDepartmentName', title: '事业部', width: 140},
                {field: 'specialItemCode', title: '专项编号', width: 140},
                {field: 'invoiceComment', title: '开票批注', width: 140},
                {field: 'isDropShip', title: '厂提', width: 140},
                {field: 'unplannedPurchase', title: '临采', width: 140},
                {field: 'postponeInvoiceStr', title: '是否暂缓开票', width: 140},
                {field: 'endDateStr', title: '开票截止', width: 140},
                {field: 'purchasePriceDifferenceStr', title: 'B1返利', width: 140},
                {field: 'onLineOrderCode', title: '外部订单号', width: 140},
                {field: 'newBusinessTypeStr', title: '业务类型', width: 140},
                {field: 'undeliveredNeedFeedbackText', title: '未提需反馈', width: 140},
                {field: 'undeliveredHaveFeedbackText', title: '未提已反馈', width: 140},
                {field: 'overTimeDeliveryReasonText', title: '未提原因', width: 140},
                {field: 'overTimeDetailReasonText', title: '未提具体原因', width: 140},
                {field: 'planOnPickupTimeStr', title: '预计提货时间', width: 140},
                {field: 'haveRiskStr', title: '有无异常/风险', width: 140},
                {field: 'contractDeliveryTimeTypeText', title: '是否期货', width: 140},
                {field: 'canvassContacts', title: '招商人员', width: 140},
                {field: 'businessContacts', title: '商务助理', width: 140},
                {field: 'settleContacts', title: '结算人员', width: 140},
                {field: 'businessManagerContacts', title: 'B1运营经理', width: 140},
                {field: 'discountCostReason', title: '折让追损费调整原因', width: 140},
                {field: 'settlementWeight', title: '结算重量', width: 140},
                {field: 'settlementAmt', title: '结算金额', width: 140},
                {field: 'soBusinessManagerName', title: '订单运营经理', width: 140},
                {field: 'isConsignment', title: '易采寄售', width: 140},
                {field: 'isCreditCompanyPallet', title: '控货白条', width: 140},
                {field: 'contractDeliveryDay', title: '交货时间', width: 140},
                {field: 'contractPickUpDay', title: '提货时间', width: 140},
                {field: 'upstreamTag', title: '指定上游', width: 140},
                {field: 'downstreamTag', title: '指定下游', width: 140},
                {field: 'isTempB2', title: '临时采购', width: 140},
                {field: 'advanceSale', title: '预售业务', width: 140},
                {field: 'dropShipSpecial', title: '厂提专项', width: 140},
                {field: 'iouZJSTag', title: '控货白条转寄售', width: 140},
                {field: 'originalOrderPaidTag', title: '原单已还款', width: 140},
                {field: 'unConsignmentSaleTag', title: '非寄售', width: 140},
                {field: 'contractVersionStr', title: '合同版本', width: 140},
                {field: 'paymentWayStr', title: '付款方式', width: 140},
                {field: 'deliverContractTypeStr', title: '合同模板', width: 140},
                {field: 'tagContent', title: '关联订单', width: 140},
                {field: 'transferStatusDesc', title: '过户状态', width: 140},
                {field: 'securityDepositReceivableAmount', title: '应收保证金', width: 140},
                {field: 'securityDepositReceivedAmount', title: '已收保证金', width: 140},
                {field: 'securityDepositReceivingAmount', title: '审核中保证金', width: 140},
                {field: 'unionFixedBindingTag', title: '联营定轧', width: 140},
                {field: 'factoryFixedBindingTag', title: '钢厂定轧', width: 140},
                {field: 'drainagePersonName', title: '订单引流人员', width: 140},
                {field: 'drainageTypeText', title: '订单引流类型', width: 140},
                {field: 'confirmStatusStr', title: '运营确认', width: 140},
                {title: '操作', fixed: 'right', toolbar: '#operatorCol'}
            ];
            return arr;
        }()],
        before: function (options) {
            // 修改或额外追加 where 属性
            options.where.searchType = '1';
            options.where.listOrCancel = 10;
            options.where.orderDateFrom = '2024-04-01 00:00:00';
            options.where.orderDateTo = '2024-04-07 23:59:59';
            options.where.postponeInvoice = '0';
            options.where.contractPickUpDayType = '0';
        },
        parseData: function (res) { // res 即为原始返回的数据
            return {
                "code": res.code === 0 && res?.result?.success === true ? 0 : -1 || -1, // 解析接口状态
                "msg": res.msg || res?.result?.message || '请求失败', // 解析提示文本
                "count": res?.result?.data?.total || 0, // 解析数据长度
                "data": res?.result?.data?.rows || [] // 解析数据列表
            };
        },
    });

    // 工具栏事件
    table.on('toolbar(mallOrderTable)', function (obj) {
        console.log(obj.event, obj)
        var id = obj.config.id;//config 是表格配置，id=mallOrderTable
        switch (obj.event) {
            case 'getCheckData':
                var data = table.checkStatus(id).data;//获取选中行
                layer.alert(util.escape(JSON.stringify(data)));
                break;
            case 'getData':
                var getData = table.getData(id);//获取当前页所有行表格数据。
                layer.alert(util.escape(JSON.stringify(getData)));
                break;
            case 'add':
                layer.open({
                    type: 1,
                    shade: [0.9, '#000'], // 遮罩
                    content: $('#mallOrderDialog'), // 捕获的元素
                    end: function () {
                        // layer.msg('关闭后的回调', {icon:6});
                    }
                });
                break;
            case 'update':
                layer.alert('暂不支持批量编辑，敬请期待！');
                break;
            case 'delete':
                var data = table.checkStatus(id).data;//获取选中行
                if (data.length == 0) {
                    layer.alert('请至少选择一行数据进行操作');
                    return;
                } else {
                    var ids = data.map(row => row.id);
                    console.log(ids);
                    layer.alert('暂不支持批量删除，敬请期待！');
                    return;
                }
                break;
        }

    });

    // 触发单元格工具事件
    table.on('tool(mallOrderTable)', function (obj) {
        var event = obj.event;
        var data = obj.data; // 获得当前行数据
        if (event === 'edit') {
            layer.alert('暂不支持编辑，敬请期待！');
        } else if (event === 'del') {
            layer.confirm('你确认要删除数据吗？', function (index) {
                del(data.id, function () {
                    layer.close(index);
                });
            })
        }
    });

    // 搜索提交
    form.on('submit(mallOrderSearch)', function (data) {
        var field = data.field; // 获得表单字段
        // 执行搜索重载
        table.reload('mallOrderTable', {
            page: {
                curr: 1 // 重新从第 1 页开始
            },
            where: field // 搜索的字段
        });
        return false; // 阻止默认 form 跳转
    });

    // 新增模版组保存事件
    form.on('submit(mallOrderDialogSave)', function (data) {
        $.ajax({
            type: "POST",
            url: "/api/mallOrder/add",
            data: JSON.stringify(data.field),
            contentType: 'application/json',
            success: function (r) {
                if (r.status == 0) {
                    layer.closeAll();
                    layer.alert('新增成功！');
                    table.reload('mallOrderTable', {});
                } else {
                    layer.alert('新增失败！' + r.msg);
                }
            }
        });
        return false; // 阻止默认 form 跳转
    });

    function del(id, callback) {
        $.ajax({
            type: "POST",
            url: "/api/mallOrder/remove/" + id,
            success: function (r) {
                if (r.status == 0) {
                    layer.alert('删除成功！');
                    table.reload('mallOrderTable', {});
                } else {
                    layer.alert('删除失败！' + r.msg);
                }
                if (callback) {
                    callback();
                }
            }
        });
    }

    exports('mallOrderList1', {});
});

